{% extends 'cms/base.html' %}

{% block title %}
    {% if news %}
        编辑新闻
    {% else %}
        发布新闻
    {% endif %}
{% endblock %}

{% block head %}
    <script src="{% static 'ueditor/ueditor.config.js' %}"></script>
    <script src="{% static 'ueditor/ueditor.all.min.js' %}"></script>
    <script src="{% static 'js/write_news.min.js' %}"></script>
{% endblock %}

{% block content-header %}
    <h1>
        {% if news %}
            编辑新闻
        {% else %}
            发布新闻
        {% endif %}
    </h1>
{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-md-12">
            <div class="card card-primary">
                <form role="form">
                    <div class="card-body">
                        <div class="form-group">
                            <label for="title-form">标题</label>
                            {% if news %}
                                <input type="text" class="form-control" id="title-form" name="title" value="{{ news.title }}">
                            {% else %}
                                <input type="text" class="form-control" id="title-form" name="title">
                            {% endif %}
                        </div>
                        <div class="form-group">
                            <label for="category-form">分类</label>
                            <select name="category" id="category-form" class="form-control">
                                {% for category in categories %}
                                    {% if news and news.category_id == category.pk %}
                                        <option value="{{ category.id }}" selected>{{ category.name }}</option>
                                    {% else %}
                                        <option value="{{ category.id }}">{{ category.name }}</option>
                                    {% endif %}

                                {% endfor %}

                            </select>
                        </div>
                        <div class="form-group">
                            <label for="desc-form">描述</label>
                            {% if news %}
                                <input type="text" class="form-control" id="desc-form" name="desc" value="{{ news.desc }}">
                            {% else %}
                                <input type="text" class="form-control" id="desc-form" name="desc">
                            {% endif %}

                        </div>
                        <div class="form-group">
                            <label for="thumbnail-form">缩略图</label>
                            <div class="input-group">
                                    {% if news %}
                                        <input type="text" class="form-control" id="thumbnail-form" name="thumbnail" value="{{ news.thumbnail }}">
                                    {% else %}
                                        <input type="text" class="form-control" id="thumbnail-form" name="thumbnail">
                                    {% endif %}

                                    <span class="input-group-btn">
                                        <label class="btn btn-default btn-file">
                                            上传图片<input hidden type="file" class="btn btn-default" id="thumbnail-btn">
                                        </label>
                                    </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="content-form">内容</label>
                            {% if news %}
                                <script id="editor" type="text/plain">{{ news.content|safe }}</script>
                            {% else %}
                                <script id="editor" type="text/plain"></script>
                            {% endif %}

                        </div>
                    </div>
                    <!-- /.card-body -->

                    <div class="card-footer">
                        <button id="submit-btn" data-news-id="{{ news.pk }}" type="submit" class="btn btn-primary">发布新闻</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
{% endblock %}
